{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "isInteractiveWindowMessageCell": true
   },
   "source": [
    "已连接到 env_py38 (Python 3.8.18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas\n",
    "df = pandas.read_csv('D:\\ProgramFile2_OR\\Python_study\\mathorcup2024C\\附件\\附件1.csv',encoding = 'utf-8')\n",
    "df['日期'] = pandas.to_datetime(df['日期'])\n",
    "\n",
    "# sortNum = [int(item[2:] for item in df['分拣中心'])]\n",
    "\n",
    "# select_sort = df.sort_values(by = '分拣中心').reset_index(drop = True)\n",
    "# data_sort = select_sort.groupby('分拣中心').apply(lambda x: x.sort_values(by = '日期').reset_index(drop = True))\n",
    "# print(data_sort.head(n=20))\n",
    "#data_sort.to_csv(r\"D:\\ProgramFile2_OR\\Python_study\\mathorcup2024C\\sortData_Fujian1.csv\", index=False, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    分拣中心         日期    货量\n",
      "0   SC48 2023-09-05   723\n",
      "1   SC48 2023-10-24  2092\n",
      "2   SC48 2023-08-05   754\n",
      "3   SC48 2023-11-16   754\n",
      "4   SC48 2023-11-09   771\n",
      "5   SC48 2023-10-29   692\n",
      "6   SC48 2023-09-08   819\n",
      "7   SC48 2023-10-19   643\n",
      "8   SC48 2023-10-23   590\n",
      "9   SC48 2023-08-20   648\n",
      "10  SC48 2023-08-30   683\n",
      "11  SC48 2023-11-01  3313\n",
      "12  SC48 2023-08-22   693\n",
      "13  SC48 2023-08-21   714\n",
      "14  SC48 2023-11-15   692\n",
      "   分拣中心         日期    货量  asSort\n",
      "0  SC48 2023-09-05   723      48\n",
      "1  SC48 2023-10-24  2092      48\n",
      "2  SC48 2023-08-05   754      48\n",
      "3  SC48 2023-11-16   754      48\n",
      "4  SC48 2023-11-09   771      48\n",
      "5  SC48 2023-10-29   692      48\n",
      "6  SC48 2023-09-08   819      48\n",
      "7  SC48 2023-10-19   643      48\n",
      "8  SC48 2023-10-23   590      48\n",
      "9  SC48 2023-08-20   648      48\n"
     ]
    }
   ],
   "source": [
    "print(df.head(15))\n",
    "sortNum = [int(item[2:]) for item in df['分拣中心']] \n",
    "df['asSort'] = sortNum\n",
    "print(df.head(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sortNum = [int(item[2:] for item in df['分拣中心'])] \n",
    "报错：df['分拣中心']返回的是一个可迭代对象，generation,因此产生一个type错误提示\n",
    "      int() argument must be a string, a bytes-like object or a number, not 'generator'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         分拣中心         日期     货量\n",
      "asSort                         \n",
      "1      0  SC1 2023-08-01  39517\n",
      "       1  SC1 2023-08-02  40008\n",
      "       2  SC1 2023-08-03  38477\n",
      "       3  SC1 2023-08-04  35773\n",
      "       4  SC1 2023-08-05  31876\n",
      "       5  SC1 2023-08-06  31319\n",
      "       6  SC1 2023-08-07  29584\n",
      "       7  SC1 2023-08-08  33034\n",
      "       8  SC1 2023-08-09  34242\n",
      "       9  SC1 2023-08-10  34123\n"
     ]
    }
   ],
   "source": [
    "select_sort = df.sort_values(by = 'asSort').reset_index(drop = True)\n",
    "data_sort = select_sort.groupby('asSort').apply(lambda x: x.sort_values(by = '日期').reset_index(drop = True))\n",
    "data_sorted = data_sort.drop(['asSort'],axis = 1)\n",
    "print(data_sorted.head(10))\n",
    "data_sorted.to_csv(r\"D:\\ProgramFile2_OR\\Python_study\\mathorcup2024C\\sortData_Fujian1.csv\", index=False, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          分拣中心         日期    货量\n",
      "asSort                         \n",
      "2      2   SC2 2023-08-03  6858\n",
      "       3   SC2 2023-08-04  6656\n",
      "       4   SC2 2023-08-05  6075\n",
      "       5   SC2 2023-08-06  5685\n",
      "       6   SC2 2023-08-07  6528\n",
      "       7   SC2 2023-08-08  7591\n",
      "       8   SC2 2023-08-09  7963\n",
      "       9   SC2 2023-08-10  7372\n",
      "       10  SC2 2023-08-11  6892\n",
      "       11  SC2 2023-08-12  6190\n",
      "       12  SC2 2023-08-13  5741\n"
     ]
    }
   ],
   "source": [
    "print(data_sorted.iloc[124:135,:])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
